<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>5.every重写</title>
</head>

<body>

  <script>
    // 原生every 类比并 有一个false返回false
    var arr = [1, 2, 3]
    var obj = {}

    var res = arr.every(function (item, i, arr) {
      console.log(item, i, arr, obj);
      return item % 2
    }, obj)
    console.log(res);


    Array.prototype.myEvery = function (fn) {
      // 拿到arr
      var arr = this
      // this指向
      var context = arguments[1]

      var res = true
      if (typeof fn === 'function') {
        for (let i = 0; i < arr.length; i++) {
          if (!fn.apply(context, [arr[i], i, arr])) {
            res = false
            break
          }
        }
      }
      return res
    }

    var res = arr.myEvery(function (item, i, arr) {
      console.log(item, i, arr);
      return item % 2
    }, obj)

    console.log(res);
  </script>

</body>

</html>